<template>
	<view class="">
		<view class="list">
			<view class="item" v-for="(item,index) in list" :key="index" @tap="navTo(item)">
				<text>{{item.title}}</text>
				<view class="arrow">
					<image src="/static/icon/left-gray-arrow.png"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return{
				cateId:1,
				list:[]
			}
		},
		onLoad(params) {
			let self=this;
			this.cateId=params.cid;
			// 从缓存里获取文章分类列表，如果缓存里有，就不用再请求，如果没有，就请求数据，放到缓存。
			let cateList=uni.getStorageSync('gArtcileCategory');
			// 根据文章分类信息，设置NavigationBarTitle 。
			for (let index in cateList) {
				if (cateList[index]['id'] == self.cateId) {
					uni.setNavigationBarTitle({
						title: cateList[index]['name']
					});
				}
			}
			if(cateList.length<=0){
				// 文章分类列表
				let articleCategoryList = this.requestUrl.articleCategory;
				this.util.request(articleCategoryList, 'post', function(res) {
					if (res.data.code == 0 && res.data.data.length > 0) {
						cateList = res.data.data;
						uni.setStorageSync('gArtcileCategory',cateList);
						
					}
				});
			}

			// 取出文章列表
			var articleListUrl = this.requestUrl.articleList;
			// 获取文章信息的参数
			var param = {
				data: {
					cid: this.cateId,
				}
			};
			this.util.request(articleListUrl,'post',function(res){
				if(res.data.code==0 && res.data.data){
					self.list=res.data.data;
				}
			},param);
		},
		methods:{
			navTo(item){
				let id = item.id;
				let cid=item.article_category_id;
				uni.navigateTo({
					url: `./articleDetail?id=${id}&cid=${cid}`,
				});
			}
		}
		
	}
</script>

<style lang="scss" scoped>
	.list{padding:30rpx;display: flex;flex-direction: column;}
	.item{padding:15rpx;color: $uni-text-color;font-size: $uni-font-size-base;border-bottom: solid 1px $uni-border-color;}
	.arrow {
		margin: 8rpx 0 0 15rpx;
		float: right;
	}
	
	.arrow image {
		width: 30rpx;
		height: 30rpx;
	}
</style>
